package com.ice.base.aop;

import com.ice.body.active.service.ActiveMainService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;


@Slf4j
@Component
@Aspect
public class ActiveValidateAspect {

    @Resource
    private ActiveMainService activeMainService;

    @Pointcut("@annotation(ActiveValidate)")
    public void ActiveValidate() {
    }


/*    @Before("execution(* com.ice.body.business.controller.OrderController.test(..))")
    public void beforeTest(JoinPoint joinPoint) {
        Object[] obj = joinPoint.getArgs();
        System.out.println("joinPoint = " + joinPoint);
        System.out.println("他来了！");
    }*/


    @Before(value = "ActiveValidate()")
    public void beforeActive(JoinPoint joinPoint) {
        Object[] obj = joinPoint.getArgs();
        Signature signature = joinPoint.getSignature();
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        if(signature.getName().equals("doLogin")){
            if(activeMainService.validateActive() == 0){
                //0 未激活
                request.setAttribute("loginFlag",0);
            }else{
                request.setAttribute("loginFlag",1);
            }
        }else{
            Class declaringType = signature.getDeclaringType();
            if (attributes != null) {

            }
        }
    }


    @AfterReturning(value = "ActiveValidate()", returning = "result")
    public void afterReturnActiveValidate(JoinPoint joinPoint, Object result) {
        String targetName = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();

        Object[] arguments = joinPoint.getArgs();
        Class targetClass = null;
        try {
            targetClass = Class.forName(targetName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        if (targetClass != null) {
            Method[] methods = targetClass.getMethods();
            String description = null;
            for (Method method : methods) {
                if (method.getName().equals(methodName)) {
                    if (method.getParameterTypes().length == arguments.length) {
                        description = method.getAnnotation(ActiveValidate.class).remark();
                        log.info(method.getName()+","+description);
                        log.info(result.toString());
                        break;
                    }
                }
            }

        }
    }
}
